package com.jianbing.north.service.impl;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Logger;

import com.jianbing.north.domain.NorthQues;
import com.jianbing.north.domain.NorthQuesOption;
import com.jianbing.north.domain.NorthQuesP3DTO;
import com.jianbing.north.mapper.NorthAnswerRecordMapper;
import com.jianbing.north.mapper.NorthQuesOptionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.north.mapper.NorthQuesStemMapper;
import com.jianbing.north.domain.NorthQuesStem;
import com.jianbing.north.service.INorthQuesStemService;

/**
 * 题干Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@Service
public class NorthQuesStemServiceImpl implements INorthQuesStemService 
{
    @Autowired
    private NorthQuesStemMapper northQuesStemMapper;
    @Autowired
    private NorthQuesOptionMapper northQuesOptionMapper;
    @Autowired
    private NorthAnswerRecordMapper northAnswerRecordMapper;
    /**
     * 查询题干
     * 
     * @param id 题干主键
     * @return 题干
     */
    @Override
    public NorthQuesStem selectNorthQuesStemById(Long id)
    {
        return northQuesStemMapper.selectNorthQuesStemById(id);
    }

    /**
     * 查询题干列表
     * 
     * @param northQuesStem 题干
     * @return 题干
     */
    @Override
    public List<NorthQuesStem> selectNorthQuesStemList(NorthQuesStem northQuesStem)
    {
        return northQuesStemMapper.selectNorthQuesStemList(northQuesStem);
    }

    /**
     * 新增题干
     * 
     * @param northQuesStem 题干
     * @return 结果
     */
    @Override
    public int insertNorthQuesStem(NorthQuesStem northQuesStem)
    {
        return northQuesStemMapper.insertNorthQuesStem(northQuesStem);
    }

    /**
     * 修改题干
     * 
     * @param northQuesStem 题干
     * @return 结果
     */
    @Override
    public int updateNorthQuesStem(NorthQuesStem northQuesStem)
    {
        return northQuesStemMapper.updateNorthQuesStem(northQuesStem);
    }

    /**
     * 批量删除题干
     * 
     * @param ids 需要删除的题干主键
     * @return 结果
     */
    @Override
    public int deleteNorthQuesStemByIds(Long[] ids)
    {
        return northQuesStemMapper.deleteNorthQuesStemByIds(ids);
    }

    /**
     * 删除题干信息
     * 
     * @param id 题干主键
     * @return 结果
     */
    @Override
    public int deleteNorthQuesStemById(Long id)
    {
        return northQuesStemMapper.deleteNorthQuesStemById(id);
    }

    @Override
    public List<NorthQues> selectNorthQuesByUserIdAndPartAndDifficulty(Long userId, Long part, Long difficulty) {
        if(northAnswerRecordMapper.selectTodayCountByUserIdAndPartAndDifficult(userId,part,difficulty)>=4){
            return null;
        }
        Long dialogueId = northQuesStemMapper.selectDialogueIdNotDoQuesStemByUserIdAndPartAndDifficult(userId,part,difficulty);
        List<NorthQues> res = northQuesStemMapper.selectQuesStemByDialogueId(dialogueId);
        for (NorthQues northQues:res){
            NorthQuesOption northQuesOption = new NorthQuesOption();
            northQuesOption.setStemId(northQues.getId());
            List<NorthQuesOption> list = northQuesOptionMapper.selectNorthQuesOptionList(northQuesOption);
            Long randAns = Long.valueOf(ThreadLocalRandom.current().nextInt(list.size()));
            Long rightId = northQues.getRightId();
            if(!randAns.equals(rightId)){
//                System.out.println("northques:"+northQues);
                northQues.setRightId(randAns);
                for(NorthQuesOption option:list){
                    if(option.getOptionId().equals(randAns)){
                        option.setOptionId(rightId);
                    } else if (option.getOptionId().equals(rightId)) {
                        option.setOptionId(randAns);
                    }
                }
            }
            northQues.setOptions(changeOptionId(list));
        }
        return res;
    }

    @Override
    public NorthQuesP3DTO selectP3QuesByUserIdAndDifficulty(Long userId, Long difficulty) {
        List<String> list = new ArrayList<>();
        NorthQuesStem northQues = northQuesStemMapper.selectUnDoP3QuesByUserIdAndDifficulty(userId, difficulty);
        String[] sentence = northQues.getStem().split("(?<=\n)");
        String tmp = "";
        for(String s:sentence){
            tmp = tmp+s;
            if(s.contains("儿童")){
                list.add(tmp);
                tmp = "";
            }
        }
        NorthQuesP3DTO northQuesP3DTO = new NorthQuesP3DTO();
        northQuesP3DTO.setDialogueId(northQues.getDialogueId());
        northQuesP3DTO.setQues(list);
        northQuesP3DTO.setScene(northQues.getScene());
        return northQuesP3DTO;
    }

    @Override
    public String selestQuesByDialogueId(Long dialogueId) {
        return northQuesStemMapper.selestQuesByDialogueId(dialogueId);
    }

    private List<NorthQuesOption> changeOptionId(List<NorthQuesOption> list){
        Map<Long,String> map = new HashMap<>();
        map.put(0L,"A：");
        map.put(1L,"B：");
        map.put(2L,"C：");
        map.put(3L,"D：");
        for (NorthQuesOption northQuesOption1: list){
            northQuesOption1.setOptionContent(map.get(northQuesOption1.getOptionId())+northQuesOption1.getOptionContent());
        }
        return list;
    }
}
